#换行符需使用LF
#!/bin/bash
#执行前先赋予执行权限
# chmod +x install.sh

# 定义软件名称变量
software_name=WampCam
service_name=WampWorker
desktop_name=DesktopCam
software_path=/opt/${software_name}
#echo ${software_name}
#echo ${service_name}
#echo ${desktop_name}
#echo ${software_path}

echo 停止服务
systemctl stop  $service_name
systemctl disable $service_name

echo 下载软件
#在这里配置下载地址
wget http://192.168.100.165:8083/${software_name}.zip && \
echo 解压zip文件到目标路径 && \
unzip -o ${software_name}.zip -d /opt   && \
echo 删除zip文件 && \
rm "${software_name}".zip

# 检查文件是否存在并赋予执行权限
[ -f "${software_path}/${service_name}" ] && chmod +x "${software_path}/${service_name}"
[ -f "${software_path}/${desktop_name}" ] && chmod +x "${software_path}/${desktop_name}"

echo 创建systemd服务文件
cat <<EOF1 > /etc/systemd/system/${service_name}.service
[Unit]
Description=${service_name}
After=network.target

[Service]
Type=notify
ExecStart=${software_path}/${service_name}
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF1

echo 重新加载systemd管理的服务
systemctl daemon-reload
systemctl start ${service_name}
systemctl enable ${service_name}

echo 创建桌面快捷方式文件
cat <<EOF2 > /usr/share/applications/${desktop_name}.desktop
[Desktop Entry]
Version=1.0
Type=Application
Name=${desktop_name}
Exec=${software_path}/${desktop_name}
Icon=${software_path}/icon.ico
Terminal=false
Categories=Utility;
EOF2

# 将快捷方式复制到桌面
cp /usr/share/applications/${desktop_name}.desktop ~/桌面/
chmod +x ~/桌面/${desktop_name}.desktop